package com.example.util

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * 时间工具类
 * @author  chaohaipeng  on 2018/7/12.
 */
/**
 * 当前年加减正为加减为负
 */
fun addYear(addTime: Int): Long {
    val c = Calendar.getInstance()
    val year = c.get(Calendar.YEAR)
    val myYear = year + addTime
    if (myYear > 0) {
        try {
            if (addTime < 0) {
                val strYear = "$myYear-01-01"
                return getTimeMill(strYear, "yyyy-MM-dd")
            } else {
                val strYear = "$myYear-12-31"
                return getTimeMill(strYear, "yyyy-MM-dd")
            }
        } catch (e: Exception) {
            return System.currentTimeMillis()
        }
    } else {
        return System.currentTimeMillis()

    }


}


/**
 * 根据模型获取时间戳
 */
@Throws(ParseException::class)
fun getTimeMill(time: String, model: String): Long {
    val format = SimpleDateFormat(model)
    val date = format.parse(time)
    return date.time
}

/**
 * 获得某个月最大天数
 *
 * @param year 年份
 * @param month 月份 (1-12)
 * @return 某个月最大天数
 */
fun getMaxDayByYearMonth(year: Int, month: Int): Int {
    val calendar = Calendar.getInstance()
    calendar.set(Calendar.YEAR, year - 1)
    calendar.set(Calendar.MONTH, month)
    return calendar.getActualMaximum(Calendar.DATE)
}